<?php 

namespace Admin\Controller;
use Think\Controller;

/**
 * 模板消息管理控制器
 * @author 艾逗笔<765532665@qq.com>
 */
class TemplateController extends BaseController {


    public function add() {
        if (IS_POST) {//自动验证
            $_validate = array(
                array('content', 'require', '消息内容不能为空', 1, 'regex', 3),
                array('plantime', 'require', '计划推送时间不能为空', 1, 'regex', 3)
            );
            $_auto = array(
                // array('good_id','get_good_name',1,'callback'),
                array('createtime', 'time', 1, 'function')
            );
            $Message = M('template');
            $Message->setProperty('_validate', $_validate);
            $Message->setProperty('_auto', $_auto);
            // $data['name'] = I('name');
            // $data['id'] = I('get.id');
            if (!$Message->create()) {
                $this->error($Message->getError());
            } else {
                $message_add = $Message->add();
                if ($message_add) {
                    $this->success('添加内容成功', U('lists'));
                } else {
                    $this->error('添加内容失败');
                }
            }
        } else {
            $this->addCrumb('系统管理', U('Index/index'), '')
                 ->addCrumb('发货管理', U('Template/lists'), '')
                 ->addCrumb('编辑内容', '', 'active')
                 ->addNav('编辑内容', '', 'active')
                 ->setModel('template')
                 ->addFormField('content', '消息内容', 'textarea')
                 // ->addFormField('good_id', '商品名称','text', I('name'))
                 ->addFormField('plantime', '计划推送时间', 'time')
                 ->common_add();
        }
    }




    public function edit() {
        if (IS_POST) {
            if (!I($content)) {
                $this->error('消息内容不能为空');
            }
            $_validate = array(
                array('content', 'require', '消息内容不能为空', 1, 'regex', 3)
            );
        
            $Message = M('template');
            $Message->setProperty('_validate', $_validate);
            $data['id'] = I('get.id');
            $data['content'] = I('content');
            $data['plantime'] = strtotime(I('plantime')); //将字符串形式的时间，转换为时间戳 -zsyk
            C('TOKEN_ON', false);
            if (!$Message->create($data)) {
                $this->error($Message->getError());
            } else {
                $rst=$Message->save($data);
                $this->success('模板内容编辑成功', U('lists'));
            }
            
        } else {
            $Message = M('template')->find(I('id'));
            $Message['downtime'] = date('Y-m-d H:i', $Message['downtime']); //将时间戳转换成字符串时间格式用于输出到JS处理 -zsyk
            $this->addCrumb('系统管理', U('Index/index'), '')
                 ->addCrumb('发货管理', U('Template/lists'), '')
                 ->addCrumb('编辑内容', '', 'active')
                 ->addNav('编辑内容', '', 'active')
                 ->setModel('template')
                 ->addFormField('content', '消息内容', 'textarea')
                 ->addFormField('plantime', '计划推送时间', 'time')
                 
                 
                 // ->addFormField('roles', '用户角色', 'checkbox', array('options'=>'callback','callback_name'=>'get_user_roles'))
                 ->setFormData($Message)
                 ->common_edit();
        }
    }


    /**
     * 模板消息列表
     * 
     */
    public function lists() {
        $this->addCrumb('系统管理', U('Index/index'), '')
             ->addCrumb('发货管理', U('Template/lists'), '')
             ->addCrumb('模板消息列表', '', 'active')
             ->addNav('模板消息列表', '', 'active')
             ->setModel('template')
             ->setListOrder('id desc')
             ->setListSearch(array(
                'number' => '订单号',
                'buytime' => '下单时间'
             ))
             ->addListItem('id', '模板消息ID')
             ->addListItem('good_id', '商品名称')
             ->addListItem('order_id', '订单号', 'callback', array('callback_name'=>'order_member'))
             ->addListItem('createtime', '添加时间', 'function', array('function_name'=>'date','params'=>'Y-m-d H:i:s,###'))
             ->addListItem('plantime', '计划推送时间', 'function', array('function_name'=>'date','params'=>'Y-m-d H:i:s,###'))
             ->addListItem('pushnum', '推送量')
             ->addListItem('status', '状态')
             ->addListItem('id', '操作', 'custom', array('options'=>array('edit'=>array('编辑内容',U('edit',array('id'=>'{id}')),'btn btn-primary btn-sm icon-edit',''),'delete'=>array('推送消息',U('delete',array('id'=>'{id}')),'btn btn-danger btn-sm icon-delete', ''))))
             ->setListPer(30)
             ->common_lists();
    }

    public function get_goods_name() {
        $results = M('goods')->select();
        foreach ($results as $k => $v) {
            $goods[$v['id']] = $v['name'];
        }
        return $goods;
    }

    public function get_user_name() {
        $results = M('user')->select();
        foreach ($results as $k => $v) {
            $users[$v['id']] = $v['username'];
        }
        return $users;
    }

    //添加模板消息添加时间
    public function get_good_name(){
        $goods = M('goods')->where(array('id'=>I('get.id')))->select();
        
        return $goods['name'];
    }
	
    public function order_member($member_id) {
        $results = M('member')->field('nickname')->where(array('id'=>$member_id))->find();
        return $results['nickname'];
    }


    /**
     * 格式化显示用户所属角色
     * @author 艾逗笔<765532665@qq.com>
     */
    public function order_goods($good_id) {
        $order_goods = M('user_good_order')->field('good_id')->where(array('good_id'=>$good_id))->find();
        foreach ($order_goods as $k => $v) {
            $good = M('goods')->where(array('id'=>$v['good_id']))->getField('name');
            $goods[] = $good;
        }
        return implode(',', $goods);
    }



    public function order_user($user_id) {
        $order_users = M('user_good_order')->field('user_id')->where(array('user_id'=>$user_id))->find();
        foreach ($order_users as $k => $v) {
            $user = M('user')->where(array('id'=>$v['user_id']))->getField('username');
            $users[] = $user;
        }
        return implode(',', $users);
    }   

}

?>